
package com.zte.claa.inficombo.csa.app.service.custparam.impl;


import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zte.claa.inficombo.csa.app.config.LocCustParamModel;
import com.zte.claa.inficombo.csa.app.dao.mysql.impl.LocCustParamModelMapper;
import com.zte.claa.inficombo.csa.app.service.custparam.LocCustParamModelService;

/**
 * @author 0283000170
 *
 */
@Service
public class LocCustParamModelServiceImpl implements LocCustParamModelService, InitializingBean{

	@Autowired
	private LocCustParamModelMapper loccustParamModelMapper;
	
	
	private static final Logger logger = LoggerFactory.getLogger(LocCustParamModelServiceImpl.class);

	
	
	@Override
	public List<LocCustParamModel> selectAll() throws Exception{
		return loccustParamModelMapper.selectAll();
	}
	
	
	@Override
	public void reloadLocCustParam() throws Exception{
		loadLocCustParam();
		logger.info("reload location custom param ok!");
	}
	
	
	@Override
	public void afterPropertiesSet() throws Exception{
		logger.info("start to load location custom param ...");
		new Thread(()->loadLocCustParam()).start();		
	}
	
	//定位定制参数加载到map中->key:projectid,CoordinateConverter
	private void loadLocCustParam() {
		List<LocCustParamModel> list = null;
		try{
			list = selectAll();	
			if(list != null && list.size() != 0){
				locCustParamMap.clear();
				for(LocCustParamModel locCustParamModel : list){					
					if(locCustParamModel.getGpsxyz() == GPS2XYZ){ //gps->xyz
						CoordinateConverter converter = new CoordinateConverter();
						converter.setLon_set(locCustParamModel.getGpslon());
						converter.setLat_set(locCustParamModel.getGpslat());
						converter.setX_set(locCustParamModel.getLocx());
						converter.setY_set(locCustParamModel.getLocy());
						converter.setPixel_set(locCustParamModel.getScale());
						converter.setZoneid(locCustParamModel.getZoneid());
						//转换成弧度
						converter.setRotation(locCustParamModel.getRotation() * Math.PI / 180);
						logger.info("loadLocCustParam zoneid : " + locCustParamModel.getZoneid());
						locCustParamMap.put(locCustParamModel.getProjectId(), converter);
					}
				}
			}else{
				locCustParamMap.clear();
			}
			logger.info("load location custom param ok!");
		}catch (Throwable e) {
			logger.error("load location custom param error : " + e.getMessage());
		}		
	}
	
	
}
